﻿using MsgPack;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Runtime.Serialization;
namespace NetWork
{
	public class UIMessage 
	{
		public short id;
		public Object o;
	}
	public delegate void MessageHandle(short id, Object msg );
	public static class MessageMap  {

		static Dictionary<int,Type> _cache;
		static Dictionary<int,MessageHandle> _handles;
		static  ArrayList _uimessagelist;
		static MessageMap ()
		{
			_cache = new Dictionary<int,Type> ();
			_handles = new Dictionary<int,MessageHandle> ();
			_uimessagelist = new ArrayList();
		}
		public static void RegisterMessage(int id, Type type,MessageHandle handle){
			if(Lookup(id))return;
			lock (_cache) {
				_cache[id] = type;
			}
			lock (_handles) {
				_handles[id] = handle;
			}
			//UnityEngine.Debug.Log(string.Format("Add id={0}, type={1}",id,type.ToString()));
		}
	
		public static void OnMessage(int id,byte[] buff)
		{
			//UnityEngine.Debug.Log("id===="+id);
			//UnityEngine.Debug.Log("OnMessage id="+Convert.ToString(id));
			//UnityEngine.Debug.Log(string.Format("OnMessage： start {0}", id));
			Type t=	GetType(id);
			if(t==null)
			{

				UnityEngine.Debug.Log("not register:"+Convert.ToString(id));
				return;
			}
			//UnityEngine.Debug.Log(t.ToString());
			ObjectPacker  packer = new ObjectPacker ();
			/*
			MessageHandle handle = GetHandle(id);
			if(handle!=null)
			{
				handle( (short)id,packer.Unpack (t,buff));
			}else{
				UnityEngine.Debug.Log("not register:"+Convert.ToString(id));
			}*/
			MessageHandle handle = GetHandle(id);
			if(handle!=null)
			{
				//UnityEngine.Debug.Log(string.Format("OnMessage： 00000 {0}", id));
				UIMessage msg = new UIMessage();
				msg.id = (short)id;
				try{
					//UnityEngine.Debug.Log(string.Format("OnMessage： 1111 {0}", id));
					msg.o = packer.Unpack (t,buff);
				}catch{
					UnityEngine.Debug.Log("OnMessage catch:"+Convert.ToString(id));
					return;
				}
				lock(_uimessagelist)
				{
					_uimessagelist.Add(msg);
				}
			}else{
				UnityEngine.Debug.Log("not register:"+Convert.ToString(id));
			}
		}
		public static void UpdateMessage()
		{
			lock(_uimessagelist)
			{
				while(_uimessagelist.Count>0){
					UIMessage msg=(UIMessage)_uimessagelist[0];
					_uimessagelist.RemoveAt(0);
					HandleUIMessage((short)msg.id, msg.o);
				}

				//foreach(UIMessage uims in _uimessagelist)
				//{				
				//	HandleUIMessage((short)uims.id, uims.o);
				//}
				//_uimessagelist.Clear();
			}
		}
		public static void HandleUIMessage(short id, Object o)
		{
			MessageHandle handle = GetHandle(id);
			if(handle!=null)
			{
				handle(id, o);
			}
		}
		public static MessageHandle GetHandle(int id){
			MessageHandle handle;
			lock (_handles) {
				if (_handles.TryGetValue (id, out handle))
				{
					return handle;
				}
			}
			return null;
		}
		public static Type GetType(int id){
			Type type;
			lock (_cache) {
				if (_cache.TryGetValue (id, out type))
				{
					//UnityEngine.Debug.Log(string.Format("GetType id={0}, type={1}",id,type.ToString()));
					return type;
				}
			}
			return null;
		}
		public static bool Lookup (int id)
		{
			Type type;
			lock (_cache) {
				if (_cache.TryGetValue (id, out type))
					return true;
			}

			return false;
		}

		public static void RemoveType (int id)
		{
			lock (_cache) {
				_cache.Remove (id);
			}
		}

		public static void Clear ()
		{
			lock (_cache) {
				_cache.Clear ();
			}
		}

	}
}
